import {ApiResult} from '@/phr/common/ApiResult';
import {HealthKnowledge} from '@/phr/service/knowledge/HealthKnowledge';
import HttpClient from '@/phr/common/HttpClient';
import {PageDTO} from '@/phr/common/PageDTO';
import {type} from 'os';

class HealthKnowledgeService {

  private LIST_ALL_NEWS_URL = 'news/api/all';
  private QUERY_NEWS_URL = 'news/api/query';
  private GET_NEWS_BY_TYPE_URL = 'news/api/type';

  //养生保健
  private HEALTH_CARE = 'A';
  //疾病科普
  private DISEASE_KNOWLEDGE = 'B';
  //养老资讯
  private OLDER_INFO = 'C';
  //综合资讯
  private MULTI_INFO = 'D';

  /**
   * 获取最新的资讯
   * @param {number} page 当前页数
   * @returns {Promise<ApiResult<HealthKnowledge[]>>}
   */
  public async getNewestArticle(page: number = 1): Promise<ApiResult<PageDTO<HealthKnowledge>>> {
    let params = {
      page: page
    };
    return HttpClient.get(this.LIST_ALL_NEWS_URL, params);
  }

  /**
   * 获得文章详情
   * @param {number} id 文章ID
   * @returns {Promise<ApiResult<HealthKnowledge>>}
   */
  public async getArticleDetail(id: number): Promise<ApiResult<HealthKnowledge>> {
    let params = {
      id: id
    };
    return HttpClient.get(this.QUERY_NEWS_URL, params);
  }

  /**
   * 获取健康保健信息
   * @param {number} page 页数
   * @returns {Promise<ApiResult<PageDTO<HealthKnowledge>>>}
   */
  public async getHealthCareInfo(page: number = 1): Promise<ApiResult<PageDTO<HealthKnowledge>>> {
    return this.getArticleByType(0, page);
  }

  /**
   * 获取疾病信息
   * @param {number} page
   * @returns {Promise<ApiResult<PageDTO<HealthKnowledge>>>}
   */
  public async getDiseaseInfo(page: number = 1): Promise<ApiResult<PageDTO<HealthKnowledge>>> {
    return this.getArticleByType(1, page)
  }

  /**
   * 获取养老资讯
   * @param {number} page 页数
   * @returns {Promise<ApiResult<PageDTO<HealthKnowledge>>>}
   */
  public async getOlderInfo(page: number = 1): Promise<ApiResult<PageDTO<HealthKnowledge>>> {
    return this.getArticleByType(2, page)
  }

  /**
   * 获取综合资讯
   * @param {number} page 页数
   * @returns {Promise<ApiResult<PageDTO<HealthKnowledge>>>}
   */
  public async getMultiInfo(page: number = 1): Promise<ApiResult<PageDTO<HealthKnowledge>>> {
    return this.getArticleByType(3, page)
  }

  /**
   * 根据类型返回文章列表
   * @param {number} typeIdx
   * @param {number} page
   * @returns {Promise<ApiResult<PageDTO<HealthKnowledge>>>}
   */
  public async getArticleByType(typeIdx: number, page: number = 1): Promise<ApiResult<PageDTO<HealthKnowledge>>> {
    let type = this.HEALTH_CARE;
    switch (typeIdx) {
      case 0:
        type = this.HEALTH_CARE;
        break;
      case 1:
        type = this.DISEASE_KNOWLEDGE;
        break;
      case 2:
        type = this.OLDER_INFO;
        break;
      case 3:
        type = this.MULTI_INFO;
        break;
      default:
        type = this.HEALTH_CARE;
    }
    let params = {
      type: type,
      page: page
    };
    return HttpClient.get(this.GET_NEWS_BY_TYPE_URL, params)
  }

}

export default new HealthKnowledgeService();